//
//  PPAlbumContentTableViewDescriptionCell.m
//  pickpic
//
//  Created by Jinxiao on 9/11/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumContentTableViewDescriptionCell.h"

#import "PPAlbumContentViewDataSource.h"

#import "UITextView+PP.h"

@interface PPAlbumContentTableViewDescriptionCell () <UITextViewDelegate>
@property (strong, nonatomic) IBOutlet UILabel *countLabel;
@end

@implementation PPAlbumContentTableViewDescriptionCell

- (void)awakeFromNib {
    [super awakeFromNib];

    _descriptionView.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"图册描述" attributes:@{NSForegroundColorAttributeName : [UIColor colorWithRGBHex:0xb2b2b2], NSFontAttributeName : [UIFont systemFontOfSize:16]}];
    _descriptionView.textContainerInset = UIEdgeInsetsMake(CGFLOAT_MIN, CGFLOAT_MIN, CGFLOAT_MIN, CGFLOAT_MIN);
    _descriptionView.returnKeyType = UIReturnKeyDone;
}

- (void)setDataSource:(PPAlbumContentViewDataSource *)dataSource {
    _dataSource = dataSource;

    _descriptionView.text = dataSource.desc;

    [self updateWordCount];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];

        return NO;
    }

    return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
    _dataSource.desc = textView.text;

    [self updateWordCount];
}

- (void)updateWordCount {
    NSInteger count = _dataSource.desc.length;

    _countLabel.text = [NSString stringWithFormat:@"%li/200", count];

    if(count > 200) {
        _countLabel.textColor = [UIColor colorWithRGBHex:0xdf1921];
    } else {
        _countLabel.textColor = [UIColor colorWithRGBHex:0xb2b2b2];
    }
}

@end
